namespace YasharEl.Infrastructure.ApplicationModel.Bootstrap.Configuration.Internals
{
    using System;
    using System.Configuration;
    using YasharEl.Infrastructure.ApplicationModel.Bootstrap.Extensibility;

    /// <summary>
    /// Default ILoadConfigurationSection
    /// </summary>
    public class LoadConfigurationSection : ILoadConfigurationSection
    {
        private readonly Func<string, ConfigurationSection> sectionProvider;

        /// <summary>
        /// Initializes a new instance of the <see cref="LoadConfigurationSection"/> class.
        /// </summary>
        /// <param name="extension">The extension.</param>
        public LoadConfigurationSection(IBootstrappingExtension extension)
        {
            var loader = extension as ILoadConfigurationSection;
            this.sectionProvider =
                section =>
                loader != null
                    ? loader.GetSection(section)
                    : (ConfigurationSection)ConfigurationManager.GetSection(section);
        }

        /// <summary>
        /// Gets a configuration section according to the specified name.
        /// </summary>
        /// <param name="sectionName">The section name.</param>
        /// <returns>
        /// The section.
        /// </returns>
        public ConfigurationSection GetSection(string sectionName)
        {
            return this.sectionProvider(sectionName);
        }
    }
}